package com.jvm.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {

    public static void main(String[] args) throws ClassNotFoundException,
            IllegalAccessException, InstantiationException, NoSuchMethodException,
            InvocationTargetException, NoSuchFieldException {
        /**
         * 获取targetObject类对象实例
         */
        Class<?> targerClass = Class.forName("com.jvm.reflect.TargetObject");
        TargetObject targetObject = (TargetObject) targerClass.newInstance();

        Method[] methods = targerClass.getDeclaredMethods();
        for(Method method : methods){
            System.out.println(method.getName());
        }

        Method publicMethod = targerClass.getDeclaredMethod("publicMethod", String.class);

        publicMethod.invoke(targetObject,"yulan");

        Field field = targerClass.getDeclaredField("value");
        //取消安全检查
        field.setAccessible(true);
        field.set(targetObject,"yulango");

        Method privateMethod = targerClass.getDeclaredMethod("privateMethod");

        privateMethod.setAccessible(true);
        privateMethod.invoke(targetObject);
    }
}
